@echo off
REM Usage:
REM genModel.bat database_name
REM Example:
REM genModel.bat library
REM Move the generated files from .\genModel to the corresponding service's model directory

REM Database configuration
set host=113.44.153.140
set port=5432
set dbname=%1
set username=postgres
set passwd=PaperTrust2024

REM Directory to generate model files
set modeldir=..\model

REM Temporary file to store table names
set tempfile=tables.tmp

REM Get all table names from the database
psql -h %host% -p %port% -U %username% -d %dbname% - -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'" -t > %tempfile%

REM Process each table
for /f "tokens=*" %%t in (%tempfile%) do (
    if not "%%t"=="table_name" (
        echo Starting to create table: %%t in database: %dbname%
        goctl model pg datasource -url="postgres://%username%:%passwd%@%host%:%port%/%dbname%" -table="%%t" -dir="%modeldir%" -cache=true --style=goZero
    )
)

REM Clean up temporary file
del %tempfile%

echo All models generated successfully.